package com.atguigu.lease.web.admin.interceptors;

import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.springmvc.LeaseException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
/*2. 检查请求头是否携带token [access_token]
          3. 没有 -》 去登录
          4. 有 -》 是否过期 ， 是否篡改 【若过期、篡改报错】
          5. 没有报错 -> 放行
         */
public class loginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取token :请求头 access_token(跟前端约定好的)
        String accessToken = request.getHeader("access_token");
        if (ObjectUtils.isEmpty(accessToken)){
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        //2.有token，检查是否过期/篡改
        JwtUtil.parseToken(accessToken);
        return true;
    }
}
